package com.woniu.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.woniu.config.AliPayConfig;
import com.woniu.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@RestController
public class AliPayController {
    @Resource
    AliPayConfig AlipayConfig;

    @Resource
    UserService userService;

    //    @RequestMapping("/alipay")
    @GetMapping("/alipay/{outTradeNo}/{subjects}/{totalAmount}/{bodys}/{uid}")
    @ApiOperation(value = "支付功能",notes = "<span style='color:red;'>点击充值支付</span>")
    @ApiImplicitParams({
            //dataType:参数类型
            //paramType:参数由哪里获取     path->从路径中获取，query->?传参，body->ajax请求
            @ApiImplicitParam(name = "outTradeNo",value = " 商户订单号，商户网站订单系统中唯一订单号，必填",dataType = "String",paramType = "path",example = "111111"),
            @ApiImplicitParam(name = "subjects",value = " // 订单名称，必填",dataType = "String",paramType = "path",example = "热门简历"),
            @ApiImplicitParam(name = "totalAmount",value = " // 付款金额，必填",dataType = "String",paramType = "path",example = "1000"),
            @ApiImplicitParam(name = "uid",value = "用户id",dataType = "String",paramType = "path",example = "6"),
            @ApiImplicitParam(name = "bodys",value = "商品描述，可空",dataType = "String",paramType = "path",example = "tom的简历"),

    })
    public String payController(@PathVariable String outTradeNo, @PathVariable String subjects, @PathVariable String totalAmount, @PathVariable  String bodys,@PathVariable String uid) throws IOException {

        Integer uudi  = Integer.valueOf(uid);
//        uudi=3;

        // 获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AliPayConfig.gatewayUrl, AliPayConfig.APP_ID, AliPayConfig.APP_PRIVATE_KEY, "json", AliPayConfig.CHARSET, AliPayConfig.ALIPAY_PUBLIC_KEY, AliPayConfig.sign_type);

        // 设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AliPayConfig.return_url);
        alipayRequest.setNotifyUrl(AliPayConfig.notify_url);

        // 商户订单号，商户网站订单系统中唯一订单号，必填
         outTradeNo = UUID.randomUUID().toString();

        String out_trade_no = outTradeNo;
        // 付款金额，必填
        String total_amount = "50";
        // 订单名称，必填
        String subject = "热门简历充值";
        // 商品描述，可空
        String body = bodys;

//        设置vip
       userService.vip(uudi);

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        // 请求
        String form = null;
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        return form;
    }
}
